home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / internet config assistant / toolkit / crect.cp < prev    next >
Encoding:
Text File  |  1996-04-07  |  2.8 KB  |  156 lines

  1.  
  2. #include "CRect.h"
  3.  
  4. #include "macros.h"
  5.  
  6.  
  7. void CRect::SetLeftTop(const CPoint pt)
  8. {
  9.     fRect.left = pt.X();
  10.     fRect.top = pt.Y();
  11. }
  12.  
  13.  
  14. void CRect::SetRightBottom(const CPoint pt)
  15. {
  16.     fRect.right = pt.X();
  17.     fRect.bottom = pt.Y();
  18. }
  19.  
  20.  
  21. void CRect::SetLeftBottom(const CPoint pt)
  22. {
  23.     fRect.left = pt.X();
  24.     fRect.bottom = pt.Y();
  25. }
  26.  
  27.  
  28. void CRect::SetRightTop(const CPoint pt)
  29. {
  30.     fRect.right = pt.X();
  31.     fRect.top = pt.Y();
  32. }
  33.  
  34.  
  35.  
  36. void CRect::InsetBy(CPoint pt)
  37. {
  38.     InsetBy(pt.X(), pt.Y());
  39. }
  40.  
  41.  
  42. void CRect::InsetBy(GraphicalUnit dx, GraphicalUnit dy)
  43. {
  44.     fRect.left += dx;
  45.     fRect.right -= dx;
  46.     fRect.top += dy;
  47.     fRect.bottom -= dy;
  48. }
  49.  
  50.  
  51. void CRect::OffsetBy(CPoint pt)
  52. {
  53.     OffsetBy(pt.X(), pt.Y());
  54. }
  55.  
  56.  
  57. void CRect::OffsetBy(GraphicalUnit dx, GraphicalUnit dy)
  58. {
  59.     fRect.left += dx;
  60.     fRect.right += dx;
  61.     fRect.top += dy;
  62.     fRect.bottom += dy;
  63. }
  64.  
  65.  
  66. void CRect::OffsetTo(CPoint pt)
  67. {
  68.     fRect.right = Width() + pt.X();
  69.     fRect.bottom = Height() + pt.Y();
  70. }
  71.  
  72.  
  73. void CRect::OffsetTo(GraphicalUnit x, GraphicalUnit y)
  74. {
  75.     fRect.right = Width() + x;
  76.     fRect.bottom = Height() + y;
  77. }
  78.  
  79.  
  80.  
  81. Boolean CRect::operator==(CRect r) const
  82. {
  83.     return (Left() == r.Left()) && (Top() == r.Top()) && (Right() == r.Right()) && (Bottom() == r.Bottom());
  84. }
  85.  
  86.  
  87. Boolean CRect::operator!=(CRect r) const
  88. {
  89.     return (Left() != r.Left()) || (Top() != r.Top()) || (Right() != r.Right()) || (Bottom() != r.Bottom());
  90. }
  91.  
  92.  
  93.  
  94. CRect CRect::operator&(CRect r) const
  95. {
  96.     return CRect(min(Left(), r.Left()), min(Top(), r.Top()), max(Right(), r.Right()), max(Bottom(), r.Bottom()));
  97. }
  98.  
  99.  
  100. CRect CRect::operator|(CRect r) const
  101. {
  102.     return CRect(max(Left(), r.Left()), max(Top(), r.Top()), min(Right(), r.Right()), min(Bottom(), r.Bottom()));
  103. }
  104.  
  105.  
  106.  
  107. Boolean CRect::Intersects(CRect r) const
  108. {
  109.     return (r.Left() < Right()) && (r.Right() > Left()) && (r.Top() < Bottom()) && (r.Bottom() > Top());
  110. }
  111.  
  112.  
  113. Boolean CRect::Contains(CPoint pt) const
  114. {    
  115.     return (pt.X() <= Right()) && (pt.X() >= Left()) && (pt.Y() <= Bottom()) && (pt.Y() >= Top());
  116. }
  117.  
  118.  
  119. Boolean CRect::Contains(CRect r) const
  120. {
  121.     return (r.Left() >= Left()) && (r.Right() <= Right()) && (r.Top() >= Top()) && (r.Bottom() <= Bottom());
  122. }
  123.  
  124. void CRect::Center(CRect outsideRect, Boolean scaleIfNeeded)
  125. {
  126.     CRect r(*this);
  127.  
  128.     if (scaleIfNeeded)
  129.     {
  130.  
  131.         // make r smallest square enclosing srcRect
  132.         if (r.Width() > r.Height())
  133.             r.SetHeight(r.Width());
  134.         else
  135.             r.SetWidth(r.Height());
  136.  
  137.         ::MapRect((Rect*)this, r, outsideRect);
  138.  
  139.         // Center the theRect inside destRect (topleft's already equal)
  140.         OffsetBy((outsideRect.Right() - Right()) / 2, (outsideRect.Bottom() - Bottom()) / 2);
  141.     }
  142.     else
  143.     {
  144.         // Center theRect uniformly inside destRect without scaling
  145.  
  146.         // Center the theRect inside destRect (topleft's already equal)
  147.         fRect.left = outsideRect.Left() + r.Width() >> 1;
  148.         fRect.right = Left() + r.Width();
  149.         fRect.top = outsideRect.Top() + r.Height() >> 1;
  150.         fRect.bottom = Left() + r.Height();
  151.     }
  152.  
  153. }
  154.  
  155.  
  156.